package com.tiennt.android.chinesechess.models;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import com.tiennt.android.chinesechess.models.quanco.QuanCo;

import com.tiennt.android.chinesechess.ui.UIPainter;

public class BanCo {
	// Debug purpose.
	private boolean DBG = true;
	private String TAG = "BanCo";

	public static final int cTRANGTHAI_BANCO_ACTIVE = 1;
	public static final int cTRANGTHAI_BANCO_INACTIVE = 2;

	public OCo[][] mOCo;
	public BitmapHolder mBitmapHolder;
	public int mTrangThai = cTRANGTHAI_BANCO_INACTIVE;
	public QuanCo mSelectedQc;

	public Player mPlayer1;
	public Player mPlayer2;

	public void toggleTrangThai() {
		if (mTrangThai == cTRANGTHAI_BANCO_ACTIVE) {
			mTrangThai = cTRANGTHAI_BANCO_INACTIVE;
		} else {
			mTrangThai = cTRANGTHAI_BANCO_ACTIVE;
		}
	}

	public BanCo(BitmapHolder bitmapHolder) {
		this.mBitmapHolder = bitmapHolder;
		mOCo = new OCo[10][9];

		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 9; j++) {
				mOCo[i][j] = new OCo(i, j);
			}
		}

		initialNewGame();
		// initialBanCoDebugging();
	}

	public void initialNewGame() {
		mPlayer1 = new Player(QuanCo.cPHE_DO, this);
		mPlayer2 = new Player(QuanCo.cPHE_DEN, this);
	}

	public Coord translateXyToCoord(float x, float y, float scaleFactor) {
		Coord result = new Coord();
		float newX = x / scaleFactor - 5;
		float newY = y / scaleFactor - 5;

		int k = (int) (newX / 80);

		boolean doX = false, doY = false;

		if (newX < 80 * k + 70) {
			result.mCot = k;
			doX = true;
		}

		k = (int) (newY / 80);

		if (newY < 80 * k + 70) {
			result.mHang = k;
			doY = true;
		}

		if (doX && doY) {
			return result;
		}

		return null;
	}

	public void log(final String message) {
		if (DBG) {
			Log.d(TAG, message);
		}
	}

	public void drawBanCo(Canvas canvas, Paint paint) {
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 9; j++) {
				QuanCo quanCo = mOCo[i][j].mQuanCo;
				if (quanCo != null) {
					quanCo.drawItself(canvas, paint, this);
				}

			}
		}
		showHints(canvas, paint);

	}

	public void showHints(Canvas canvas, Paint paint) {
		ArrayList<Coord> hints = new ArrayList<Coord>();

		if (mSelectedQc != null) {
			hints = mSelectedQc.calculatePosibleMoves(this);
		}

		for (Coord c : hints) {
			UIPainter.drawHint(canvas, paint, c.mCot, c.mHang);
		}
	}

	public boolean isBiChieu(int pheCuaTuongCanKiemTra) {
		Player enemy;
		Player leMe;

		if (mPlayer1.mPhe == pheCuaTuongCanKiemTra) {
			enemy = mPlayer2;
			leMe = mPlayer1;
		} else {
			enemy = mPlayer1;
			leMe = mPlayer2;
		}

		Coord tuongPos = new Coord(leMe.mTuong.mHang, leMe.mTuong.mCot);
		log("Phe " + pheCuaTuongCanKiemTra);
		log(tuongPos.toString());
		if (!enemy.mTot[0].isDead()
				&& enemy.mTot[0].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mTot[1].isDead()
				&& enemy.mTot[1].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mTot[2].isDead()
				&& enemy.mTot[2].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mTot[3].isDead()
				&& enemy.mTot[3].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mPhao[0].isDead()
				&& enemy.mPhao[0].calculatePosibleMoves(this)
						.contains(tuongPos)) {
			return true;
		} else if (!enemy.mPhao[1].isDead()
				&& enemy.mPhao[1].calculatePosibleMoves(this)
						.contains(tuongPos)) {
			return true;
		} else if (!enemy.mXe[0].isDead()
				&& enemy.mXe[0].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mXe[1].isDead()
				&& enemy.mXe[1].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mMa[0].isDead()
				&& enemy.mMa[0].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mMa[1].isDead()
				&& enemy.mMa[1].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mTinh[0].isDead()
				&& enemy.mTinh[0].calculatePosibleMoves(this)
						.contains(tuongPos)) {
			return true;
		} else if (!enemy.mTinh[1].isDead()
				&& enemy.mTinh[1].calculatePosibleMoves(this)
						.contains(tuongPos)) {
			return true;
		} else if (!enemy.mSy[0].isDead()
				&& enemy.mSy[0].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		} else if (!enemy.mSy[1].isDead()
				&& enemy.mSy[1].calculatePosibleMoves(this).contains(tuongPos)) {
			return true;
		}

		log("Bi Chieu: false");
		return false;
	}

	public boolean isChongTuong() {
		log("Cot tuong 1: " + mPlayer1.mTuong.mCot);
		log("Cot tuong 2: " + mPlayer2.mTuong.mCot);
		if (mPlayer1.mTuong.mTrangThai == QuanCo.cTRANGTHAI_BIAN
				|| mPlayer2.mTuong.mTrangThai == QuanCo.cTRANGTHAI_BIAN) {
			return false;
		}
		if (mPlayer1.mTuong.mCot == mPlayer2.mTuong.mCot) {
			int col = mPlayer1.mTuong.mCot;
			int row = mPlayer1.mTuong.mHang + 1;

			while (row < mPlayer2.mTuong.mHang) {
				if (mOCo[row][col].mQuanCo != null) {
					log("Chong tuong false");
					return false;
				}
				row++;
			}
			log("Chong tuong true");
			return true;
		}
		log("Chong tuong false");
		return false;
	}

	public int isGameOver() {
		if (mPlayer1.mTuong.mTrangThai == QuanCo.cTRANGTHAI_BIAN)
			return mPlayer1.mPhe;
		else if (mPlayer2.mTuong.mTrangThai == QuanCo.cTRANGTHAI_BIAN) {
			return mPlayer2.mPhe;
		}
		return 0;
	}
}
